/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */

#ifndef __SKY_DOME_H__
#define __SKY_DOME_H__

#include "SceneObject.h"

namespace PQEngine
{

	class SkyDomeProperty
	{
	public:
		void setup(Ogre::GpuProgramParametersSharedPtr vertexPtr, Ogre::GpuProgramParametersSharedPtr fragmentPtr)
		{
			_vertexPtr=vertexPtr;
			_fragmentPtr=fragmentPtr;
		}

		int getPhysicalIndex(Ogre::GpuProgramParametersSharedPtr ptr,Ogre::String name)
		{
			assert(!ptr.isNull());
			const Ogre::GpuConstantDefinition* def=ptr->_findNamedConstantDefinition(name);
			if(def){
				return def->physicalIndex;
			}
			return -1;
		}

		void setHazeColor(const Ogre::ColourValue& color)
		{
			_hazeColor=color;
		}

	private:
		Ogre::GpuProgramParametersSharedPtr _vertexPtr;
		Ogre::GpuProgramParametersSharedPtr _fragmentPtr;
		Ogre::ColourValue _hazeColor;
	};

	class SkyDome
	{
	public:
	
		static const int MESH_SEGMENTS=32;
		static const Ogre::String SCENE_NODE_NAME;
		static const Ogre::String ENTITY_NAME;
		static const Ogre::String MESH_NAME;
		static const Ogre::String MATERIAL_NAME;
		static const Ogre::String MATERIAL_CLONE_NAME;
		
		enum Type {
            TYPE_SKY_DOME,
            TYPE_IMAGE_STAR_FIELD,
        };

	public:
		SkyDome(SceneObject* sceneObject,Type=TYPE_SKY_DOME);
		void update(float deltaT);

		void enable(bool enabled);

		void setFarRadius(Ogre::Real radius);
		void setSunDirection(const Ogre::Vector3& sunDir);
        void setHazeColor(const Ogre::ColourValue& color);
	private:
		void setHaze(bool enabled);
		void initMaterial();
		void generateDomeGeometry();
		void initDomeBuffer(float *pVertex, unsigned short *pIndices, int segments);
		void initStarFeildBuffer(float *pVertex, unsigned short *pIndices, int segments);
	private:
		SceneObject* _sceneObject;
		Ogre::SceneManager* _sceneManager;
		Ogre::MaterialPtr _material;//Sky dome's material
		Ogre::Entity* _entity;
		Ogre::SceneNode* _sceneNode;
		
		SkyDomeProperty _property;
		Type _type;
		bool _shaderEnabled;
		bool _hazeEnabled;

		Ogre::GpuProgramParametersSharedPtr _vertexPtr;
		Ogre::GpuProgramParametersSharedPtr _fragmentPtr;

		bool _enabled;
	};
}
#endif
